package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.StickerAdapter;
import flc.ast.bean.MyStickerBean;
import flc.ast.databinding.ActivityAddStickerBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class AddStickerActivity extends BaseAc<ActivityAddStickerBinding> {

    public static String imgPath = "";
    private StickerAdapter stickerAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_sticker;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAddStickerBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAddStickerSave.setOnClickListener(this);

        mDataBinding.rvStickerList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        stickerAdapter = new StickerAdapter();
        mDataBinding.rvStickerList.setAdapter(stickerAdapter);
        stickerAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAddStickerSave://保存
                mDataBinding.ivStickerView.setShowHelpToolFlag(false);
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        stickerAdapter.getItem(oldPosition).setSelect(false);
        stickerAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        stickerAdapter.getItem(position).setSelect(true);
        stickerAdapter.notifyItemChanged(position);
        Resources res = getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, stickerAdapter.getItem(position).getImg());
        mDataBinding.ivStickerView.addBitImage(bmp);
    }

    @Override
    protected void initData() {
        //获取图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.imageViewTouch);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        //贴纸数据
        List<MyStickerBean> listSticker = new ArrayList<>();
        listSticker.add(new MyStickerBean(R.drawable.tz_1, getString(R.string.sticker_name, 1), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_2, getString(R.string.sticker_name, 2), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_3, getString(R.string.sticker_name, 3), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_4, getString(R.string.sticker_name, 4), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_5, getString(R.string.sticker_name, 5), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_6, getString(R.string.sticker_name, 6), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_7, getString(R.string.sticker_name, 7), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_8, getString(R.string.sticker_name, 8), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_9, getString(R.string.sticker_name, 9), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_10, getString(R.string.sticker_name, 10), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_11, getString(R.string.sticker_name, 11), false));
        listSticker.add(new MyStickerBean(R.drawable.tz_12, getString(R.string.sticker_name, 12), false));
        stickerAdapter.setList(listSticker);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlStickerSave);
                        //保存到历史记录
                        String resultFile = FileUtil.generateFilePath(Content.myRecord, ".png");
                        ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}